package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.dubbo.api.mongo.SoundApi;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.Sound;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@DubboService
public class SoundApiImpl implements SoundApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private FriendApi friendApi;

    /**
     * 保存语音
     *
     * @param sound
     */
    @Override
    public void save(Sound sound) {
        mongoTemplate.insert(sound);
    }

    /**
     * 获得一条语音
     *
     * @param userId
     * @param gender
     * @param redisIds
     * @return
     */
    @Override
    public Sound getOne(Long userId, String gender, Set<String> redisIds) {
        //排除好友ids
        Query friendQuery = new Query(Criteria.where("userId").is(userId));
        List<Friend> friends = mongoTemplate.find(friendQuery, Friend.class);
        List<Long> friendIds = friends.stream().map(Friend::getFriendId).collect(Collectors.toList());
        //排除不喜欢的用户ids
        Query userLikeQuery = new Query(Criteria.where("userId").is(userId).and("isLike").is(false));
        List<UserLike> userLikes = mongoTemplate.find(userLikeQuery, UserLike.class);
        List<Long> userLikeIds = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        List<Long> excludeIds = CollectionUtil.unionAll(friendIds, userLikeIds);
        //排除自己的id
        excludeIds.add(userId);

        Criteria criteria = new Criteria();
        criteria.and("userId").nin(excludeIds);
        criteria.and("gender").ne(gender);
        criteria.and("id").nin(redisIds);

        //查询时使用随机取样, mongodb的聚合统计
        TypedAggregation<Sound> soundTypedAggregation = TypedAggregation.newAggregation(Sound.class, Aggregation.match(criteria), Aggregation.sample(1));
        // p1: 统计参数， p2: 返回 值的类型
        AggregationResults<Sound> aggregate = mongoTemplate.aggregate(soundTypedAggregation, Sound.class);
        if(CollectionUtils.isEmpty(aggregate.getMappedResults()))
            return null;
        Sound sound = aggregate.getMappedResults().get(0);
        //保存到soundReceive表
/*        SoundReceive soundReceive = new SoundReceive();
        soundReceive.setSoundId(sound.getId());
        soundReceive.setUserId(userId);
        soundReceive.setUserSendId(sound.getUserId());
        soundReceive.setCreated(System.currentTimeMillis());
        mongoTemplate.insert(soundReceive);*/
        return sound;
    }
}
